package org.yinweichen.llm_dataset_backend.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yinweichen.llm_dataset_backend.DTO.CandidateAnswerCreateDTO;
import org.yinweichen.llm_dataset_backend.DTO.CandidateAnswerUpdateDTO;
import org.yinweichen.llm_dataset_backend.entity.CandidateAnswer;
import org.yinweichen.llm_dataset_backend.entity.CandidateAnswerId;
import org.yinweichen.llm_dataset_backend.entity.StandardQuestion;
import org.yinweichen.llm_dataset_backend.entity.StandardQuestionId;
import org.yinweichen.llm_dataset_backend.repository.CandidateAnswerRepository;
import org.yinweichen.llm_dataset_backend.repository.StandardQuestionRepository;
import org.yinweichen.llm_dataset_backend.service.CandidateAnswerService;
import org.yinweichen.llm_dataset_backend.util.IdGenerator;

import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional
public class CandidateAnswerServiceImpl implements CandidateAnswerService {
    private final CandidateAnswerRepository candidateAnswerRepository;
    private final StandardQuestionRepository standardQuestionRepository;
    private final IdGenerator idGenerator;

    @Override
    @Transactional
    public ResponseEntity<CandidateAnswer> create(CandidateAnswerCreateDTO request) {
        try {
            StandardQuestion question = standardQuestionRepository.findById(
                            new StandardQuestionId(request.standardQuestionId(),request.version()))
                .orElseThrow(() -> new IllegalArgumentException("未找到对应的问题"));

            CandidateAnswer answer = CandidateAnswer.builder()
                .id(idGenerator.generateCandidateAnswerId())
                .version(request.version())
                .answer(request.answer())
                .author(request.author())
                .standardQuestion(question)
                .updateTime(new Date())
                .build();

            CandidateAnswer savedAnswer = candidateAnswerRepository.save(answer);
            return ResponseEntity.ok(savedAnswer);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    @Override
    @Transactional(readOnly = true)
    public ResponseEntity<List<CandidateAnswer>> getByQuestion(Long questionId, String version) {
        try {
            List<CandidateAnswer> answers = candidateAnswerRepository
                .findByStandardQuestionIdAndStandardQuestionVersion(questionId, version);
            return ResponseEntity.ok(answers);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    @Override
    @Transactional
    public ResponseEntity<?> update(CandidateAnswerUpdateDTO request) {
        try {
            CandidateAnswer answer = candidateAnswerRepository.findById(
                            new CandidateAnswerId(request.id(),
                    request.version()))
                .orElseThrow(() -> new IllegalArgumentException("未找到对应的候选回答"));
            candidateAnswerRepository.update(request.id(), request.version(),request.answer());
            return ResponseEntity.ok("更新成功");
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }

    @Override
    @Transactional
    public ResponseEntity<Void> delete(Long id, String version) {
        try {
            candidateAnswerRepository.deleteById(new CandidateAnswerId(id, version));
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }
}